package com.xsz.controller;

import com.xsz.dto.ResumeDTO;
import com.xsz.util.ResultVOUtil;
import com.xsz.util.WordUtil;
import com.xsz.vo.ResultVO;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/job")
@Slf4j
@RestController
public class JobController {


    @Value("${fileStoreRootPath}")
    String filePath;
    /**
     *  图文+表格-个人简介
     *
     *  生成个人简历
     * @return
     */
    @RequestMapping("/resume")
    public ResultVO<Map<String, String>> createResume(@RequestBody  ResumeDTO resumeDTO){
        String finalFilePath="";
        try {
            Map<String,Object> dataMap = new HashMap<String,Object>();
            dataMap.put("f1", resumeDTO.getName());
            dataMap.put("f2", resumeDTO.getSex());
            dataMap.put("f3", resumeDTO.getBorn());
            dataMap.put("f4", resumeDTO.getNation());
            dataMap.put("f5", resumeDTO.getEducation());
            dataMap.put("f6", resumeDTO.getMajor());
            dataMap.put("f7", resumeDTO.getAddress());
            dataMap.put("f8", resumeDTO.getEmailaddress());
            dataMap.put("f9", resumeDTO.getMobile());
            dataMap.put("f10", resumeDTO.getWorkedyears());
            dataMap.put("f11", resumeDTO.getCertificate());
            dataMap.put("f12", resumeDTO.getTrainExp());
            dataMap.put("f13", resumeDTO.getAssessment());
            dataMap.put("f14", resumeDTO.getProjectExp());
            //图片
            // dataMap.put("image1", getImageBase("C:\\tmp1\\123.jpg"));
            System.out.println(resumeDTO.getImage1().substring(10));
            dataMap.put("image2", WordUtil.getImageBase("C:\\tmp\\"+resumeDTO.getImage1().substring(10)));


            Configuration configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");
            //指定模板路径的第二种方式,我的路径是D:/      还有其他方式
            System.out.println("filePath==="+filePath);
            configuration.setDirectoryForTemplateLoading(new File(filePath));

            // 输出文档路径及名称
            finalFilePath=filePath+File.separator+resumeDTO.getId()+"个人简历.doc";
            File outFile = new File(finalFilePath);
            //以utf-8的编码读取ftl文件
            Template t =  configuration.getTemplate("m5.ftl","utf-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
            t.process(dataMap, out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("filePath", finalFilePath);

        return ResultVOUtil.success(map);
    }
}
